//
//  CloseAreaViewController.m
//  PrintPort
//
//  Created by 华谕 on 2017/11/16.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "CloseAreaViewController.h"

static NSString *cellpath = @"addressThreeCell";
@interface CloseAreaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *addressAry;
@property (nonatomic, strong)NSArray *cityAry;
@property (nonatomic, strong)NSArray *areaAry;

@property (nonatomic, assign)NSInteger levelType;
@property (nonatomic, strong)AddressModel *provceModel;
@property (nonatomic, strong)AddressModel *cityModel;
@property (nonatomic, strong)NSString *titleStr;

@end

@implementation CloseAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleStr = @"选择省份";
    
    self.navigationItem.title = _titleStr;
    self.view.backgroundColor = RGB(250, 250, 250);
    _levelType = 1;
    [self creatUI];
    [self selectedAddress:@"1" ParentId:@""];
    
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = BacksColor;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
}

- (void)button1Ac
{
    if (_levelType == 1)
    {
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    else if (_levelType == 2)
    {
        _levelType = 1;
        [self.tableview reloadData];
        self.navigationItem.title = @"选择省份";
        
    }
    else
    {
        _levelType = 2;
        [self.tableview reloadData];
        self.navigationItem.title = @"选择城市";
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_levelType == 1)
    {
        return _addressAry.count;
        
    }
    else if (_levelType == 2)
    {
        return _cityAry.count;

    }
    return _areaAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellpath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellpath];
    }
    if (_levelType == 1)
    {
        if (_addressAry)
        {
            AddressModel *model = _addressAry[indexPath.row];
            cell.textLabel.text = model.name;
            cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
            cell.textLabel.textColor = RGB(77, 77, 77);
        }
    }
    else if (_levelType == 2)
    {
        if (_cityAry)
        {
            AddressModel *model = _cityAry[indexPath.row];
            cell.textLabel.text = model.name;
            cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
            cell.textLabel.textColor = RGB(77, 77, 77);
        }
    }
    else
    {
        if (_areaAry)
        {
            AddressModel *model = _areaAry[indexPath.row];
            cell.textLabel.text = model.name;
            cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
            cell.textLabel.textColor = RGB(77, 77, 77);
        }
    }
    
    UIView *lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    lineview.backgroundColor = RGB(250, 250, 250);
    [cell.contentView addSubview:lineview];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_levelType == 1)
    {
        AddressModel *model = _addressAry[indexPath.row];
        _provceModel = model;
        [self selectedAddress:@"2" ParentId:model.ID];
    }
    else if (_levelType == 2)
    {
        AddressModel *model = _cityAry[indexPath.row];
        _cityModel = model;
        [self selectedAddress:@"3" ParentId:model.ID];
    
    }
    else
    {
        AddressModel *model = _areaAry[indexPath.row];
        if (self.chooseblock != nil)
        {
            self.chooseblock(_provceModel, _cityModel, model);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

- (void)saveAddress:(NSString *)ID
{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 41*displayheight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (void)selectedAddress:(NSString *)level ParentId:(NSString *)parentId
{
    WEAKSELF;
    NSDictionary *dic;
    if ([level isEqualToString:@"1"])
    {
        dic = @{@"level":level
                };
    }
    else
    {
        dic = @{@"parentId":parentId
                };
    }
    
    [HYBaseHandler requestAreaParmers:dic Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             if ([level isEqualToString:@"1"])
             {
                 _levelType = 1;
                 _titleStr = @"选择省份";
                 _addressAry = [AddressModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
                 
             }
             else if ([level isEqualToString:@"2"])
             {
                 _levelType = 2;
                 _titleStr = @"选择城市";
                 _cityAry = [AddressModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
                 
             }
             else
             {
                 _levelType = 3;
                 _titleStr = @"选择区域";
                 _areaAry = [AddressModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             }
             dispatch_async(dispatch_get_main_queue(), ^{
                 weakSelf.navigationItem.title = _titleStr;
                 [weakSelf.tableview reloadData];
                 [weakSelf.tableview setContentOffset:CGPointMake(0,0) animated:YES];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:self.view];
             
         }
     } failed:^(NSError *error) {
         
     }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
